using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Animation;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Java.Security;
using String = System.String;

namespace CPXamarin
{
    public class KenBurnsSupportView:FrameLayout
    {
        private static String TAG = "KenBurnsView";
        private  Handler mHandler;
        private int mResourceIds;
        private ImageView[] mImageViews;
        private int mActiveImageIndex = -1;
        private Random random = new Random();
        private int mSwapMs = 10000;
        private int mFadeInOutMs = 400;
        private float maxScaleFactor = 1.5F;
        private float minScaleFactor = 1.2F;
        
        public KenBurnsSupportView(Context context):base(context,null)
        {

        }
        public KenBurnsSupportView(Context context, IAttributeSet attrs):base(context,attrs,0) 
        {

        }
        public KenBurnsSupportView(Context context, IAttributeSet attrs, int defStyle):base(context,attrs,0) 
        {
            mHandler = new Handler();
        }
        public void SetResourceIds(int resourceIds) 
        {
            mResourceIds= resourceIds;
            FillImageViews();
        }

       
          

       

         private void  SwapImage() 
         {
            if(mActiveImageIndex == -1) 
            {
                mActiveImageIndex = 1;
                Animate(mImageViews[mActiveImageIndex]);
                return;
            }

            int inactiveIndex = mActiveImageIndex;
            mActiveImageIndex = (1 + mActiveImageIndex) % mImageViews.Length;
            ImageView activeImageView = mImageViews[mActiveImageIndex];
            activeImageView.Alpha =0.0f;
            ImageView inactiveImageView = mImageViews[inactiveIndex];
            Animate(activeImageView);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.SetDuration(mFadeInOutMs);
            animatorSet.PlayTogether(
            ObjectAnimator.OfFloat(inactiveImageView, "alpha", 1.0f, 0.0f),
            ObjectAnimator.OfFloat(activeImageView, "alpha", 0.0f, 1.0f)
            );
              animatorSet.Start();
        }


        private void Start(View view, long duration, float fromScale, float toScale, float fromTranslationX, float fromTranslationY, float toTranslationX, float toTranslationY) 
        {
            view.ScaleX = fromScale;
            view.ScaleY=fromScale;
            view.TranslationX=fromTranslationX;
            view.TranslationY=fromTranslationY;
            ViewPropertyAnimator propertyAnimator = view.Animate().TranslationX(toTranslationX).TranslationY(toTranslationY).ScaleX(toScale).ScaleY(toScale).SetDuration(duration);
            propertyAnimator.Start();
            Console.WriteLine(TAG, "starting Ken Burns animation " + propertyAnimator);
        }
        private float pickScale() 
        {
            return this.minScaleFactor + this.random.Next() * (this.maxScaleFactor - this.minScaleFactor);
        }
        private float pickTranslation(int value, float ratio) 
        {
            return value * (ratio - 1.0f) * (this.random.Next() - 0.5f);
        }
        public void Animate(View view) 
        {
            float fromScale = pickScale();
            float toScale = pickScale();
            float fromTranslationX = pickTranslation(view.Width, fromScale);
            float fromTranslationY = pickTranslation(view.Height, fromScale);
            float toTranslationX = pickTranslation(view.Width, toScale);
            float toTranslationY = pickTranslation(view.Height, toScale);
            Start(view, this.mSwapMs, fromScale, toScale, fromTranslationX, fromTranslationY, toTranslationX, toTranslationY);
        }
        
        protected override void OnAttachedToWindow() 
        {
            base.OnAttachedToWindow();
            StartKenBurnsAnimation();
        }

        private void StartKenBurnsAnimation() 
        {
        }
        
        protected override void OnFinishInflate() 
        {
            base.OnFinishInflate();
            View view = Inflate(Context, Resource.Layout.view_kenburns, this);
            mImageViews = new ImageView[2];
            mImageViews[0] = view.FindViewById<ImageView>(Resource.Id.image0);
            mImageViews[1] = view.FindViewById<ImageView>(Resource.Id.image1);
        }
        private void FillImageViews() 
        {
          
            mImageViews[0].SetImageResource(mResourceIds);
        
        }
    }
}